import { Member } from './member-model.js';
import { Sms } from '../../utils/sms.js';
var member = new Member();
var sms = new Sms();
Page({
  data: {
    show_send_code: true,
    countDownNum:60,
    args:{},
    btnType:'disable',
    btnText:"立即绑定",
    onLoadding:false
  },
  onLoad: function (options) {
    this.data.setPassword = options.setPassword ? options.setPassword : ''; //绑定后是否需要进一步设置支付密码
    this.data.entry = options.entry ? options.entry : 0; //入口1从开通分销商进入
    //如果从订单入口进来，获取订单参数，以便设置完成后跳转回去
    this.data.type = options.type ? options.type : '';
    this.data.id = options.id ? options.id : '';
  },
  //提交表单
  formSubmit: function (e) {
    if (!this.data.args.mobile || !this.data.args.verifyCode) {
      wx.showToast({
        title: '请先填写手机号和验证码',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    this.sub();
  },
  //绑定手机
  sub: function () {
    var that = this;
    var params =  that.data.args;
    if(this.data.onLoadding){
      return false;
    }
    this.setData({btnText:'正在绑定...'})
    this.data.onLoadding = true;
    member.binding(params, (data) => {
      that.data.onLoadding = false;
      this.setData({ btnText: '立即绑定' })
      if (data.status == 1) {
        wx.showToast({
          title: "绑定成功",
          icon: 'success',
          duration: 2000,
          success:function(){
            var user = wx.getStorageSync('userInfo');
            user.mobile = that.data.args.mobile;
            wx.setStorageSync('userInfo', user);
            if(that.data.entry == 1){
              var url = '../fenxiao/binding';
            }else{
              if (that.data.setPassword == 1) {
                var url = "set-payment?type=" + that.data.type + '&id=' + that.data.id;
              } else {
                var url = '../center/center';
              }
            }
            
            setTimeout(function () {
              if (url == '../center/center'){
                wx.switchTab({
                  url: url,
                })
              }else{
                wx.navigateTo({
                  url: url
                });
              }
            }, 2000)
          }
        })
      } else {
        wx.showToast({
          title: data.msg,
          icon: 'none',
          duration: 2000
        })
      }
    });
  },
  //发送短信验证码
  sendCode: function (e) {
    if (!this.data.args.mobile){
      wx.showToast({
        title: '请先填写手机号码',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    this.setData({
      show_send_code : false,
      countDownNum : 60
    });
    this.countDown();
    var params ={
      mobile : this.data.args.mobile,
      scene : 1
    }
    sms.send(params, (data) => {
      if(data.status == 1){
        wx.showToast({
          title: "验证码已发送到您手机上",
          icon: 'none',
          duration: 2000
        })
      }else{
        wx.showToast({
          title: data.msg,
          icon: 'none',
          duration: 2000
        })
      }
    });
  },
  countDown: function () {
    let that = this;
    let countDownNum = that.data.countDownNum;//获取倒计时初始值
    //如果将定时器设置在外面，那么用户就看不到countDownNum的数值动态变化，所以要把定时器存进data里面
    that.setData({
      timer: setInterval(function () {//这里把setInterval赋值给变量名为timer的变量
        //每隔一秒countDownNum就减一，实现同步
        countDownNum--;
        that.setData({
          countDownNum: countDownNum
        })
        //在倒计时还未到0时，这中间可以做其他的事情，按项目需求来
        if (countDownNum == 0) {
          clearInterval(that.data.timer);
          that.setData({
            show_send_code: true
          });
        }
      }, 1000)
    })
  },
  //获取用户输入的手机号码内容
  mobileChange: function (e) {
    var val = e.detail.value;
    this.setData({
      'args.mobile': val
    });
    this.btnChange();
  },
  //获取用户输入的验证码内容
  codeChange: function (e) {
    var val = e.detail.value;
    this.setData({
      'args.verifyCode': val
    });
    this.btnChange();

  },
  //判断激活按钮的样式
  btnChange: function () {
    let type = 'disable'
    if (this.data.args.mobile && this.data.args.verifyCode) {
      type = 'sub';
    }
    this.setData({
      btnType: type
    });
  }
})